package leetcode.algorithm.dynamicPlan;

import org.testng.annotations.Test;

import java.util.*;
import java.util.stream.Collectors;

class lockDemo1{
    @Test
    public void longestConsecutive() {
        int[] nums =  {0,3,7,2,5,8,4,6,0,1};
        Arrays.sort(nums);
        List<Integer> list = new ArrayList<>();
        for (int num : nums) {
            list.add(num);
        }
        List<Integer> collect = list.stream().sorted().distinct().collect(Collectors.toList());
        System.out.println(list);
        int cur = collect.get(0), sum = 1, MAX_SUM = 0;
        for (int i = 0; i < collect.size()-1; i++) {
            if(cur+1 == collect.get(i+1)){
                sum++;
                cur++;
            }
            else {
                MAX_SUM = Math.max(MAX_SUM, sum);
                sum = 0;
                cur = collect.get(i+1);
            }
        }
        System.out.println(MAX_SUM);

    }

}

public class numsQianTao {
    List<Integer> list = new ArrayList<>();
    public static void main(String[] args) {

        numsQianTao numsQianTao = new numsQianTao();
        int[] nums = {5,4,0,3,1,6};
        int n= nums.length;
        int[] res = new int[n];


//        System.out.println(nums.length+" "+ nums.length/2);
//        System.out.println(numsQianTao.arrayNesting(nums));

    }

    public int arrayNesting(int[] nums) {

        int n = nums.length, res = 0;
        boolean[] flag = new boolean[n];
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < n; ++i){
            int cur = 0;
            while(!flag[i]){
                flag[i]=true;
                i=nums[i];
                cur++;
            }
            res = Math.max(res, cur);
        }
        return res;
    }





    public int arrayNesting1() {
        int[] nums = {5,4,0,3,1};

        Set<Integer> set = new HashSet<>();
        int res = 0;
        for(int i=0;i<nums.length;i++){
            int cur = 0;
            cur = queryRepeat(i,nums,set,cur);
            res=Math.max(res,cur);
            set.clear();
        }
        return res;
    }
    public int queryRepeat(int i, int[] nums, Set<Integer> set, int cur){
        if(i>=0&&i<nums.length&&set.add(i)){
            cur++;
            i=nums[i];
            cur = queryRepeat(i,nums,set,cur);
        }else return cur;
        return cur;

    }
}
